package org.xwalk.core;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;



public class XWalkHttpAuthHandler   implements XWalkHttpAuth  {

    private ArrayList<Object> constructorTypes;
    private ArrayList<Object> constructorParams;
    private ReflectMethod postWrapperMethod;


    private XWalkCoreWrapper coreWrapper;
    private Object bridge;

    protected Object getBridge() {
        return bridge;
    }

    public XWalkHttpAuthHandler(Object bridge) {
        this.bridge = bridge;
        reflectionInit();
    }


    public void proceed(String username, String password) {
        try {
            proceedStringStringMethod.invoke(username, password);
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        
    }

    private ReflectMethod proceedStringStringMethod = new ReflectMethod(null, "proceed");


    public void cancel() {
        try {
            cancelMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        
    }

    private ReflectMethod cancelMethod = new ReflectMethod(null, "cancel");


    public boolean isFirstAttempt() {
        try {
            return (Boolean)isFirstAttemptMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return false;
    }

    private ReflectMethod isFirstAttemptMethod = new ReflectMethod(null, "isFirstAttempt");


    void reflectionInit() {
        XWalkCoreWrapper.initEmbeddedMode();

        coreWrapper = XWalkCoreWrapper.getInstance();
        if (coreWrapper == null) {
            XWalkCoreWrapper.reserveReflectObject(this);
            return;
        }

        proceedStringStringMethod.init(bridge, null,
                "proceedSuper", String.class, String.class);
        cancelMethod.init(bridge, null,
                "cancelSuper");
        isFirstAttemptMethod.init(bridge, null,
                "isFirstAttemptSuper");
    }

}
